package com.dwjf.core.app;

import java.util.HashMap;

public class Configurator {

    private static final HashMap<String,Object> LATTE_CONFIGS = new HashMap<>();

    private Configurator(){
        LATTE_CONFIGS.put(ConfigType.CONFIG_READY.name(), false);
    }

    private static class Holder{
        private static final Configurator INSTANCE = new Configurator();
    }

    public static Configurator getInstance(){
        return Holder.INSTANCE;
    }

    public final void  configure(){
        LATTE_CONFIGS.put(ConfigType.CONFIG_READY.name(), true);
    }


    public HashMap<String, Object> getLatteConfigs() {
        return LATTE_CONFIGS;
    }


    private void checkConfiguration(){
        final boolean isReady = (Boolean) LATTE_CONFIGS.get(ConfigType.CONFIG_READY.name());
        if (!isReady){
            throw new RuntimeException("Configuration is not read");
        }
    }

    final <T> T getConfiguration(Enum<ConfigType> key){
        checkConfiguration();
        return (T)LATTE_CONFIGS.get(key.name());
    }

    /**
     * 配置 host
     * @param host
     * @return
     */
    public final Configurator withApiHost(String host){
        LATTE_CONFIGS.put(ConfigType.API_HOST.name(), host);
        return this;
    }

}
